namespace CSharpPro._1016;
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
public class User
{
    private List<Account> accounts = new List<Account>();

    public void TestPrint7()
    {
        bool exit = false;
        while (!exit)
        {
            Console.WriteLine("主菜单：1.登录 2.注册 3.退出");
            Console.Write("请选择功能：");
            string choice = Console.ReadLine();

            switch (choice)
            {
                case "1":
                    Login();
                    break;
                case "2":
                    Register();
                    break;
                case "3":
                    exit = true;
                    Console.WriteLine("感谢使用，再见！");
                    break;
                default:
                    Console.WriteLine("请输入有效数字！");
                    break;
            }
        }
    }

    private void Register()
    {
        Console.Write("请输入用户名（3-15个字符，仅含字母、数字、下划线）：");
        string username = Console.ReadLine().Trim();
        if (string.IsNullOrEmpty(username))
        {
            Console.WriteLine("用户名不可为空！");
            return;
        }
        if (!Regex.IsMatch(username, @"^[a-zA-Z0-9_]{3,15}$"))
        {
            Console.WriteLine("用户名格式错误！长度需3-15个字符，且只能包含字母、数字、下划线。");
            return;
        }
        if (accounts.Exists(a => a.Username == username))
        {
            Console.WriteLine("用户名已存在！");
            return;
        }

        Console.Write("请输入密码（6-20个字符，需包含至少1个大写字母、1个小写字母和1个数字）：");
        string password = Console.ReadLine();
        if (string.IsNullOrEmpty(password))
        {
            Console.WriteLine("密码不可为空！");
            return;
        }
        if (password.Length < 6 || password.Length > 20)
        {
            Console.WriteLine("密码长度需为6-20个字符！");
            return;
        }
        if (!Regex.IsMatch(password, @"^(?=.*[A-Z])(?=.*[a-z])(?=.*\d).+$"))
        {
            Console.WriteLine("密码需包含至少1个大写字母、1个小写字母和1个数字！");
            return;
        }

        Console.Write("请确认密码：");
        string confirmPwd = Console.ReadLine();
        if (password != confirmPwd)
        {
            Console.WriteLine("两次密码输入不一致！");
            return;
        }

        accounts.Add(new Account { Username = username, Password = password });
        Console.WriteLine("注册成功！");
    }

    private void Login()
    {
        Console.Write("请输入用户名：");
        string username = Console.ReadLine().Trim();
        Console.Write("请输入密码：");
        string password = Console.ReadLine();

        Account account = accounts.Find(a => a.Username == username && a.Password == password);
        if (account != null)
        {
            Console.WriteLine("登录成功！进入系统首页（模拟）");
        }
        else if (accounts.Exists(a => a.Username == username))
        {
            Console.WriteLine("密码错误！");
        }
        else
        {
            Console.WriteLine("用户名不存在！");
        }
    }

    private class Account
    {
        public string Username { get; set; }
        public string Password { get; set; }
    }
}